package com.hz.springboot02.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hz.springboot02.pojo.Provider;
import com.hz.springboot02.pojo.ProviderRequest;
import com.hz.springboot02.service.ProviderService;
import com.hz.springboot02.service.impl.ProviderServiceImpl;
import com.hz.springboot02.utils.MessageJson;
import com.hz.springboot02.utils.ResultLayUi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName ProviderController
 * @Description 供应商接口
 * @Author Administrator
 * @Date 2024/12/3 10:00
 */
@RestController
@RequestMapping("/pro")
@CrossOrigin(origins = "*")
public class ProviderController {
    @Autowired
    ProviderService providerService ;
    @Resource(name = "provider")
    private Provider provider;

    @GetMapping("/providerIndex")//Integer page, Integer limit, , String proDesc, String beginDate, String endDate
    public ResultLayUi providerIndex(){

        return ResultLayUi.success(30,providerService.list());
//        return providerService.listPage(page,limit,proCode,proName,proDesc,beginDate,endDate);
    }
    @GetMapping("/delPro")
    public MessageJson delPro(Integer id){
        return providerService.removeById(id)?MessageJson.success():MessageJson.error();
    }

    @PostMapping("/delPros")
    public MessageJson delPros(String ids){
        // 使用split方法将字符串按逗号分隔
        String[] idArray = ids.split(",");
        // List<String>转换为 List<Integer>
        List<Integer> integerList = new ArrayList<>();
        // 将数组转换为List
        String[] idList = idArray;
        for (String s : idList) {
            integerList.add(Integer.parseInt(s));
        }
        return providerService.removeByIds(integerList)?MessageJson.success():MessageJson.error();
    }
    @GetMapping("/selectById")
    public MessageJson selectById(Integer id){

        Provider provider =  providerService.getById(id);
        if (provider!=null){
            return MessageJson.success(provider);
        }else {
            return MessageJson.error();
        }
    }

    @GetMapping("/proList")
    public MessageJson proList(){
        return MessageJson.success(providerService.proList());
    }
    @PostMapping("/updatePro")
    public MessageJson updatePro(@RequestBody ProviderRequest providerRequest){
        Provider provider = providerRequest.getProvider();
        Provider provider1 = new Provider();
        provider1.setId(provider.getId());
        provider1.setProCode(provider.getProCode());
        provider1.setProName(provider.getProName());
        return MessageJson.successOrError(providerService.updateById(provider1));
    }

    @PostMapping("/addPro")
    public MessageJson addPro(@RequestBody ProviderRequest providerRequest){
        Provider provider = providerRequest.getProvider();
        return MessageJson.successOrError(providerService.save(provider));
    }


//    @PostMapping("/editProOrAddPro")
//    public MessageJson editProOrAddPro(Provider provider){
//
//        return providerService.editProOrAddPro(provider);
//    }
//
//    @GetMapping(value = "/add",produces = "application/json;charset=utf-8")
//    public MessageJson addProTest(){
//
//      return  providerService.editProOrAddPro(provider);
//    }

}
